{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc-hr-collapsed": false
   },
   "source": [
    "# Pandas 操作指南\n",
    "\n",
    "这份 notebook 是我在学习、使用 pandas 时记下的一些笔记，发布于此方便自己随时查阅。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 工具函数\n",
    "\n",
    "class display:\n",
    "    \"\"\"Display HTML representation of multiple objects\"\"\"\n",
    "    \n",
    "    template = \"\"\"<div style=\"font-size: 13px;float:left;margin-right:10px;\">{0}</div>\"\"\"\n",
    "    \n",
    "    def __init__(self, *args):\n",
    "        self.args = args\n",
    "        \n",
    "    def _repr_html_(self):\n",
    "        html_list = []\n",
    "        for var_name in self.args:\n",
    "            var = eval(var_name)\n",
    "            if hasattr(var, '_repr_html_'):\n",
    "                html = \"<p>{}<p/>{}\".format(var_name, var._repr_html_())\n",
    "            else:\n",
    "                html = '<pre>{}</pre>'.format(var_name + '\\n\\n' + repr(var))\n",
    "            html_list.append(self.template.format(html))\n",
    "        return '\\n'.join(html_list)\n",
    "        \n",
    "    def __repr__(self):\n",
    "        return '\\n\\n'.join(self.template.format(repr(eval(a)))\n",
    "                           for a in self.args)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Series\n",
    "\n",
    "Series 是 one-dimensional array-like object，它包含一组数据，以及这组数据中每个元素对应的标签。如下例所示，其中第一列为标签(`index`)，第二列为数据(`values`)。\n",
    "\n",
    "### 创建 Series\n",
    "\n",
    "=> 使用数组创建 Series\n",
    "\n",
    "```python\n",
    "s = pd.Series([4,5,6,7])\n",
    "```\n",
    "\n",
    "=> 访问 index 和 value\n",
    "\n",
    "```python\n",
    "s.values, s.index\n",
    ">>> (array([4, 5, 6, 7]), RangeIndex(start=0, stop=4, step=1))\n",
    "```\n",
    "\n",
    "=> 指定 index\n",
    "\n",
    "```python\n",
    "s = pd.Series([4,5,6,7], index=['a','b','c','d'])\n",
    "s.values, s.index\n",
    ">>> (array([4, 5, 6, 7]), Index(['a', 'b', 'c', 'd'], dtype='object'))\n",
    "```\n",
    "\n",
    "创建 Series 时，如果只提供数据，没有提供 index，那么 index 就默认从 0 开始递增。也可以在创建 Series 时明确指定 index。\n",
    "\n",
    "=> 使用字典创建 Series\n",
    "\n",
    "此时字典的 key, value 分别作为 Series 的 index, value\n",
    "\n",
    "```python\n",
    "dct = {'a': 4, 'b': 5, 'c': 6, 'd': 7}\n",
    "pd.Series(dct)\n",
    ">>>\n",
    "a    4\n",
    "b    5\n",
    "c    6\n",
    "d    7\n",
    "dtype: int64\n",
    "```\n",
    "\n",
    "使用字典创建 Series 时，若指定 index 则只会选 dict 中指定的 key, value 对。\n",
    "\n",
    "```python\n",
    "pd.Series(dct, index=['b', 'c', 'e'])\n",
    ">>>\n",
    "b    5.0\n",
    "c    6.0\n",
    "e    NaN\n",
    "dtype: float64\n",
    "```\n",
    "\n",
    "### 访问 Series\n",
    "\n",
    "=> 用 index 或下标访问 Series\n",
    "\n",
    "可以使用 index 来访问，也可以使用下标来访问。在访问时，会先尝试使用 index，如果 index 没有匹配的，会尝试使用下标。\n",
    "\n",
    "```python\n",
    "s['a'], s[0]\n",
    ">>> (4, 4)\n",
    "```\n",
    "\n",
    "=> 一次访问多个元素\n",
    "\n",
    "一次访问多个元素时，返回的结果依然是 Series。\n",
    "\n",
    "```python\n",
    "s = pd.Series([4,5,6,7], index=['a','b','c','d'])\n",
    "s[[1, 2]]\n",
    ">>> \n",
    "b    5\n",
    "c    6\n",
    "dtype: int64\n",
    "```\n",
    "\n",
    "\n",
    "=> 把 Series 作为字典使用\n",
    "\n",
    "```python\n",
    "s = pd.Series([4,5,6,7], index=['a','b','c','d'])\n",
    "\n",
    "'a' in s\n",
    ">>> True\n",
    "\n",
    "s.keys()\n",
    ">>> Index(['a', 'b', 'c', 'd'], dtype='object')\n",
    "\n",
    "s.values\n",
    ">>> array([4, 5, 6, 7])\n",
    "\n",
    "list(s.items())\n",
    ">>> [('a', 4), ('b', 5), ('c', 6), ('d', 7)]\n",
    "```\n",
    "\n",
    "=> 把 Series 作为一维数组使用\n",
    "\n",
    "Series 也可以使用类似数组中的切片，切片可以在 index 上进行，也可以在下标上进行。\n",
    "\n",
    "```python\n",
    "s = pd.Series([4,5,6,7], index=['a','b','c','d'])\n",
    "\n",
    "s['a': 'c']\n",
    ">>>\n",
    "a    4\n",
    "b    5\n",
    "c    6\n",
    "dtype: int64\n",
    "    \n",
    "s[0: 2]\n",
    ">>>\n",
    "a    4\n",
    "b    5\n",
    "dtype: int64\n",
    "```\n",
    "\n",
    "### 使用 `loc`, `iloc` 来对 Series 进行访问\n",
    "\n",
    "可以使用命名的 index 访问 Series，比如 `s['a']`。也可以通过下标来访问，比如 `s[1]`。那么当 Series 用数字作为 index 时，常常会引起混乱。\n",
    "\n",
    "```python\n",
    "s = pd.Series(['a', 'b', 'c'], index=[1, 3, 5])\n",
    "s\n",
    ">>>\n",
    "1    a\n",
    "3    b\n",
    "5    c\n",
    "dtype: object\n",
    "\n",
    "\n",
    "s[1]\n",
    ">>> 'a'\n",
    "\n",
    "s[0: 2]\n",
    ">>>\n",
    "1    a\n",
    "3    b\n",
    "dtype: object\n",
    "```\n",
    "\n",
    "`s[1]` 中 `1` 被视为 index，而 `s[0: 2]` 中，使用下标。因为存在以上会导致混乱的情况，Pandas 提供了一些行为更为明确的接口。\n",
    "\n",
    "\n",
    "=> 始终用 index 来访问 Series:\n",
    "\n",
    "```python\n",
    "s.loc[1]\n",
    ">>> 'a'\n",
    "\n",
    "s.loc[1: 3]\n",
    ">>>\n",
    "1    a\n",
    "3    b\n",
    "dtype: object\n",
    "```\n",
    "\n",
    "=> 明确使用下标来访问:\n",
    "\n",
    "```python\n",
    "s.iloc[1]\n",
    ">>> 'b'\n",
    "\n",
    "s.iloc[1:3]\n",
    ">>>\n",
    "3    b\n",
    "5    c\n",
    "dtype: object\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s2>10\n",
       "\n",
       "a     True\n",
       "b     True\n",
       "c    False\n",
       "d    False\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>np.sqrt(s2)\n",
       "\n",
       "a    3.741657\n",
       "b    3.872983\n",
       "c    2.449490\n",
       "d    2.645751\n",
       "dtype: float64</pre></div>"
      ],
      "text/plain": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a     True\n",
       "b     True\n",
       "c    False\n",
       "d    False\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a    3.741657\n",
       "b    3.872983\n",
       "c    2.449490\n",
       "d    2.645751\n",
       "dtype: float64</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display('s2>10', 'np.sqrt(s2)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>pd.isnull(s4)\n",
       "\n",
       "California     True\n",
       "Ohio          False\n",
       "Oregon        False\n",
       "Texas         False\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>pd.notnull(s4)\n",
       "\n",
       "California    False\n",
       "Ohio           True\n",
       "Oregon         True\n",
       "Texas          True\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s4.isnull()\n",
       "\n",
       "California     True\n",
       "Ohio          False\n",
       "Oregon        False\n",
       "Texas         False\n",
       "dtype: bool</pre></div>"
      ],
      "text/plain": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">California     True\n",
       "Ohio          False\n",
       "Oregon        False\n",
       "Texas         False\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">California    False\n",
       "Ohio           True\n",
       "Oregon         True\n",
       "Texas          True\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">California     True\n",
       "Ohio          False\n",
       "Oregon        False\n",
       "Texas         False\n",
       "dtype: bool</div>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display('pd.isnull(s4)', 'pd.notnull(s4)', 's4.isnull()')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "两个 Series 相加："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s3\n",
       "\n",
       "Ohio      35000\n",
       "Texas     71000\n",
       "Oregon    16000\n",
       "Utah       5000\n",
       "dtype: int64</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s4\n",
       "\n",
       "California        NaN\n",
       "Ohio          35000.0\n",
       "Oregon        16000.0\n",
       "Texas         71000.0\n",
       "dtype: float64</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s3+s4\n",
       "\n",
       "California         NaN\n",
       "Ohio           70000.0\n",
       "Oregon         32000.0\n",
       "Texas         142000.0\n",
       "Utah               NaN\n",
       "dtype: float64</pre></div>"
      ],
      "text/plain": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">Ohio      35000\n",
       "Texas     71000\n",
       "Oregon    16000\n",
       "Utah       5000\n",
       "dtype: int64</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">California        NaN\n",
       "Ohio          35000.0\n",
       "Oregon        16000.0\n",
       "Texas         71000.0\n",
       "dtype: float64</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">California         NaN\n",
       "Ohio           70000.0\n",
       "Oregon         32000.0\n",
       "Texas         142000.0\n",
       "Utah               NaN\n",
       "dtype: float64</div>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display('s3', 's4', 's3+s4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Series 和 Series.index 都可以用一个 name，这在和 pandas 的某些函数集成的过程中会有用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "state\n",
       "California        NaN\n",
       "Ohio          35000.0\n",
       "Oregon        16000.0\n",
       "Texas         71000.0\n",
       "Name: population, dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s4.name='population'\n",
    "s4.index.name='state'\n",
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s5>0.3\n",
       "\n",
       "a    False\n",
       "b     True\n",
       "c     True\n",
       "d     True\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s5<0.8\n",
       "\n",
       "a     True\n",
       "b     True\n",
       "c     True\n",
       "d    False\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>(s5>0.3)&(s5<0.8)\n",
       "\n",
       "a    False\n",
       "b     True\n",
       "c     True\n",
       "d    False\n",
       "dtype: bool</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>s5[(s5>0.3)&(s5<0.8)]\n",
       "\n",
       "b    0.50\n",
       "c    0.75\n",
       "dtype: float64</pre></div>"
      ],
      "text/plain": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a    False\n",
       "b     True\n",
       "c     True\n",
       "d     True\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a     True\n",
       "b     True\n",
       "c     True\n",
       "d    False\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a    False\n",
       "b     True\n",
       "c     True\n",
       "d    False\n",
       "dtype: bool</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">b    0.50\n",
       "c    0.75\n",
       "dtype: float64</div>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# masking\n",
    "\n",
    "display('s5>0.3', 's5<0.8', '(s5>0.3)&(s5<0.8)', 's5[(s5>0.3)&(s5<0.8)]')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DataFrame\n",
    "\n",
    "DataFrame 像是一个表格，每一列和每一行都有索引，通过行列索引可以对应到每一个单元格。\n",
    "\n",
    "### 创建 DataFrame\n",
    "\n",
    "DataFrame 的创建方式有很多，下表进行了总结：\n",
    "\n",
    "![](https://wangyu-name.oss-cn-hangzhou.aliyuncs.com/superbed/2019/05/23/5ce62e18697df1fd0c34e240.jpg)\n",
    "\n",
    "_来源于 Python for Data Analysis_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>state</th>\n",
       "      <th>year</th>\n",
       "      <th>pop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Ohio</td>\n",
       "      <td>2000</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Ohio</td>\n",
       "      <td>2001</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Ohio</td>\n",
       "      <td>2002</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Nevada</td>\n",
       "      <td>2001</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Nevada</td>\n",
       "      <td>2002</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    state  year  pop\n",
       "0    Ohio  2000  1.5\n",
       "1    Ohio  2001  1.7\n",
       "2    Ohio  2002  3.6\n",
       "3  Nevada  2001  2.4\n",
       "4  Nevada  2002  2.9"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\n",
    "    'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],\n",
    "    'year':  [2000, 2001, 2002, 2001, 2002],\n",
    "    'pop':   [1.5, 1.7, 3.6, 2.4, 2.9]\n",
    "}\n",
    "\n",
    "frame = pd.DataFrame(data)\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop\n",
       "0  2000    Ohio  1.5\n",
       "1  2001    Ohio  1.7\n",
       "2  2002    Ohio  3.6\n",
       "3  2001  Nevada  2.4\n",
       "4  2002  Nevada  2.9"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pd.DataFrame(data, columns=['year', 'state', 'pop'])\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop\n",
       "a  2000    Ohio  1.5\n",
       "b  2001    Ohio  1.7\n",
       "c  2002    Ohio  3.6\n",
       "d  2001  Nevada  2.4\n",
       "e  2002  Nevada  2.9"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = pd.DataFrame(data, columns=['year', 'state', 'pop'],\n",
    "                     index=['a','b','c','d','e'])\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['year', 'state', 'pop'], dtype='object')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>frame['year']\n",
       "\n",
       "a    2000\n",
       "b    2001\n",
       "c    2002\n",
       "d    2001\n",
       "e    2002\n",
       "Name: year, dtype: int64</pre></div>\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\"><pre>frame.year\n",
       "\n",
       "a    2000\n",
       "b    2001\n",
       "c    2002\n",
       "d    2001\n",
       "e    2002\n",
       "Name: year, dtype: int64</pre></div>"
      ],
      "text/plain": [
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a    2000\n",
       "b    2001\n",
       "c    2002\n",
       "d    2001\n",
       "e    2002\n",
       "Name: year, dtype: int64</div>\n",
       "\n",
       "<div style=\"font-size: 13px;float:left;margin-right:10px;\">a    2000\n",
       "b    2001\n",
       "c    2002\n",
       "d    2001\n",
       "e    2002\n",
       "Name: year, dtype: int64</div>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display(\"frame['year']\", 'frame.year')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "需要注意的是，如 `frame.pop`，因为 `pop` 是 DataFrame 的一个方法，要想访问 `pop` 这一列，就必须得 `frame['pop']` 这样写。\n",
    "\n",
    "可以很轻易地给 DataFrame 增加一列并赋值。赋值遵循广播机制。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop  debt\n",
       "a  2000    Ohio  1.5    16\n",
       "b  2001    Ohio  1.7    16\n",
       "c  2002    Ohio  3.6    16\n",
       "d  2001  Nevada  2.4    16\n",
       "e  2002  Nevada  2.9    16"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame['debt'] = 16\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop  debt\n",
       "a  2000    Ohio  1.5     0\n",
       "b  2001    Ohio  1.7     1\n",
       "c  2002    Ohio  3.6     2\n",
       "d  2001  Nevada  2.4     3\n",
       "e  2002  Nevada  2.9     4"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果使用列表进行赋值，长度要匹配\n",
    "frame['debt'] = np.arange(5)\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "      <th>debt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "      <td>3.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop  debt\n",
       "a  2000    Ohio  1.5   3.4\n",
       "b  2001    Ohio  1.7   NaN\n",
       "c  2002    Ohio  3.6   1.2\n",
       "d  2001  Nevada  2.4   NaN\n",
       "e  2002  Nevada  2.9   NaN"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以使用 Series 进行赋值，此时会根据 index 来匹配\n",
    "debt = pd.Series([1.2, 3.4, 9.0], index=['c', 'a', 'f'])\n",
    "frame['debt'] = debt\n",
    "frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['year', 'state', 'pop'], dtype='object')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以使用 del 删除某一列\n",
    "\n",
    "del frame['debt']\n",
    "frame.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['year', 'state', 'pop'], dtype='object')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2000, 'Ohio', 1.5],\n",
       "       [2001, 'Ohio', 1.7],\n",
       "       [2002, 'Ohio', 3.6],\n",
       "       [2001, 'Nevada', 2.4],\n",
       "       [2002, 'Nevada', 2.9]], dtype=object)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrame 底层是 numpy 的多维数组，很多针对数组的方法也都可以运用在 DataFrame 上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "      <th>e</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <td>2000</td>\n",
       "      <td>2001</td>\n",
       "      <td>2002</td>\n",
       "      <td>2001</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <td>Ohio</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>Nevada</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pop</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.7</td>\n",
       "      <td>3.6</td>\n",
       "      <td>2.4</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a     b     c       d       e\n",
       "year   2000  2001  2002    2001    2002\n",
       "state  Ohio  Ohio  Ohio  Nevada  Nevada\n",
       "pop     1.5   1.7   3.6     2.4     2.9"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行转置\n",
    "frame.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 对 DataFrame 的访问\n",
    "\n",
    "- `.loc`  基于标签进行索引\n",
    "- `.iloc` 基于位置进行索引\n",
    "\n",
    "=> 访问前 3 行\n",
    "\n",
    "```python\n",
    "df.iloc[:3, :]\n",
    "```\n",
    "\n",
    "=> 访问前 3 列\n",
    "\n",
    "```python\n",
    "df.iloc[:, :3]\n",
    "```\n",
    "\n",
    "=> 访问 `year` 列的前 2 行\n",
    "\n",
    "```python\n",
    "df.loc[:2, 'year']\n",
    "```\n",
    "\n",
    "=> 访问多列\n",
    "\n",
    "```python\n",
    "df.loc[:'c', ['state', 'pop']]\n",
    "```\n",
    "\n",
    "=> 使用 masked select\n",
    "\n",
    "\n",
    "```python\n",
    "# 选择满足 `df['pop'] > 2` 的行\n",
    "df.loc[df['pop'] > 2, ['pop', 'state']]\n",
    "```\n",
    "\n",
    "=> 注意事项\n",
    "\n",
    "直接在 DataFrame 上使用切片时，是对行进行切片\n",
    "\n",
    "```python\n",
    "df['a':'c']\n",
    "```\n",
    "\n",
    "不使用切片时，是对列进行访问\n",
    "\n",
    "```python\n",
    "df[['pop', 'state']]\n",
    "```\n",
    "\n",
    "### 对 DataFrame 的修改\n",
    "\n",
    "=> 修改单个 cell\n",
    "\n",
    "```python\n",
    "name_col_index = list(df.columns).index('name')\n",
    " \n",
    "df.iloc[1, name_col_index] = '老李'\n",
    "```\n",
    "\n",
    "### 删除行或列\n",
    "\n",
    "使用 `drop` 方法可以删除 DataFrame 的行或列，该方法返回一个新的 DataFrame 实例。可以通过 `axis` 参数来说明是删除的维度。\n",
    "\n",
    "=> 删除行\n",
    "\n",
    "删除对应 index\n",
    "\n",
    "```python\n",
    "df.drop(['a', 'c'])\n",
    "```\n",
    "\n",
    "=> 按列名删除列\n",
    "\n",
    "```python\n",
    "df.drop(['pop'], axis=1)\n",
    "```\n",
    "\n",
    "=> 按索引删除列\n",
    "\n",
    "```python\n",
    "df.drop(df.columns[x], axis=1)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a  c\n",
      "x  1  2\n",
      "y  3  4\n"
     ]
    }
   ],
   "source": [
    "df = pd.DataFrame([[1,2],[3,4]], columns=['a', 'c'], index=['x', 'y'])\n",
    "df\n",
    ">>>\n",
    "   a  c\n",
    "x  1  2\n",
    "y  3  4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Index Objects\n",
    "\n",
    "Series 的 index 是一个 `pd.Index` 对象，而且 `pd.Index` 是不可变对象。它包含很多方法，详细列表可以参考[这里](https://devdocs.io/pandas~0.23-index/)。\n",
    "\n",
    "\n",
    "\n",
    "根据这些方法，能够"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = frame.index\n",
    "new_index = index.append(pd.Index(['f']))\n",
    "new_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['f'], dtype='object')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_index.difference(index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop\n",
       "a  2000    Ohio  1.5\n",
       "b  2001    Ohio  1.7\n",
       "c  2002    Ohio  3.6\n",
       "d  2001  Nevada  2.4\n",
       "e  2002  Nevada  2.9"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reindex\n",
    "\n",
    "现在假设要对中各行以 `pop` 递增的顺序排列。这就需要在 DataFrame 上使用 `reindex` 方法。`reindex` 方法接受一个新的数组或者 Index 对象作为参数，DataFrame 中各行会以此重新排列。因此只需要构造出新的 index 即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>state</th>\n",
       "      <th>pop</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2000</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>2001</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>1.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2001</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2002</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>2002</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>3.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year   state  pop\n",
       "a  2000    Ohio  1.5\n",
       "b  2001    Ohio  1.7\n",
       "d  2001  Nevada  2.4\n",
       "e  2002  Nevada  2.9\n",
       "c  2002    Ohio  3.6"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_index = frame['pop'].argsort().values\n",
    "new_index = frame.index[row_index]\n",
    "frame = frame.reindex(new_index)\n",
    "frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要想调整列的顺序也可以使用 `reindex`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pop</th>\n",
       "      <th>state</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.5</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>1.7</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>2.4</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>e</th>\n",
       "      <td>2.9</td>\n",
       "      <td>Nevada</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>3.6</td>\n",
       "      <td>Ohio</td>\n",
       "      <td>2002</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   pop   state  year\n",
       "a  1.5    Ohio  2000\n",
       "b  1.7    Ohio  2001\n",
       "d  2.4  Nevada  2001\n",
       "e  2.9  Nevada  2002\n",
       "c  3.6    Ohio  2002"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame = frame.reindex(columns=['pop', 'state', 'year'])\n",
    "frame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 操作 DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>0.502710</td>\n",
       "      <td>-0.777302</td>\n",
       "      <td>-1.061957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.176748</td>\n",
       "      <td>0.615648</td>\n",
       "      <td>-1.786709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>1.180515</td>\n",
       "      <td>0.253940</td>\n",
       "      <td>0.228130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>-0.876626</td>\n",
       "      <td>0.408096</td>\n",
       "      <td>1.451826</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c\n",
       "w  0.502710 -0.777302 -1.061957\n",
       "x -0.176748  0.615648 -1.786709\n",
       "y  1.180515  0.253940  0.228130\n",
       "z -0.876626  0.408096  1.451826"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_1 = pd.DataFrame(np.random.randn(4, 3), columns=list('abc'), index=list('wxyz'))\n",
    "frame_2 = pd.DataFrame(np.random.randn(3, 3), columns=list('abd'), index=list('xyz'))\n",
    "frame_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.065928</td>\n",
       "      <td>-0.402935</td>\n",
       "      <td>-0.074010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-1.528736</td>\n",
       "      <td>1.136733</td>\n",
       "      <td>1.349797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>-0.045845</td>\n",
       "      <td>0.766156</td>\n",
       "      <td>-0.381358</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         d\n",
       "x  0.065928 -0.402935 -0.074010\n",
       "y -1.528736  1.136733  1.349797\n",
       "z -0.045845  0.766156 -0.381358"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.110820</td>\n",
       "      <td>0.212712</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-0.348221</td>\n",
       "      <td>1.390673</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>-0.922471</td>\n",
       "      <td>1.174251</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b   c   d\n",
       "w       NaN       NaN NaN NaN\n",
       "x -0.110820  0.212712 NaN NaN\n",
       "y -0.348221  1.390673 NaN NaN\n",
       "z -0.922471  1.174251 NaN NaN"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_1 + frame_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "两个 DataFrame 直接相加，如果某个单元格并非两个 DataFrame 都有，结果就会是 NaN。为了避免这种局面可以使用 `add` 方法，并提供 `fill_value` 参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>0.502710</td>\n",
       "      <td>-0.777302</td>\n",
       "      <td>-1.061957</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.110820</td>\n",
       "      <td>0.212712</td>\n",
       "      <td>-1.786709</td>\n",
       "      <td>-0.074010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-0.348221</td>\n",
       "      <td>1.390673</td>\n",
       "      <td>0.228130</td>\n",
       "      <td>1.349797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>-0.922471</td>\n",
       "      <td>1.174251</td>\n",
       "      <td>1.451826</td>\n",
       "      <td>-0.381358</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d\n",
       "w  0.502710 -0.777302 -1.061957       NaN\n",
       "x -0.110820  0.212712 -1.786709 -0.074010\n",
       "y -0.348221  1.390673  0.228130  1.349797\n",
       "z -0.922471  1.174251  1.451826 -0.381358"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_1.add(frame_2, fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里结果中依然存在一个 NAN，那是因为 fill_value 只在做加和的两者中存在一个缺失时，才会起作用。可以再使用 `fillna` 对缺失值填充。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>w</th>\n",
       "      <td>0.502710</td>\n",
       "      <td>-0.777302</td>\n",
       "      <td>-1.061957</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>-0.110820</td>\n",
       "      <td>0.212712</td>\n",
       "      <td>-1.786709</td>\n",
       "      <td>-0.074010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>-0.348221</td>\n",
       "      <td>1.390673</td>\n",
       "      <td>0.228130</td>\n",
       "      <td>1.349797</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>z</th>\n",
       "      <td>-0.922471</td>\n",
       "      <td>1.174251</td>\n",
       "      <td>1.451826</td>\n",
       "      <td>-0.381358</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a         b         c         d\n",
       "w  0.502710 -0.777302 -1.061957  0.000000\n",
       "x -0.110820  0.212712 -1.786709 -0.074010\n",
       "y -0.348221  1.390673  0.228130  1.349797\n",
       "z -0.922471  1.174251  1.451826 -0.381358"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame_1.add(frame_2, fill_value=0).fillna(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关于空值\n",
    "\n",
    "Python 中使用 None 表示空值，但是 numpy 中 `None` 无法转换为一个数字，因此一个数组中含有 `None` 则会被转换为 object 类型。\n",
    "\n",
    "对于 object 类型，无法使用 numpy 底层的 C 实现来加速，关于这个数组的所有运算都会在 Python 层面进行，性能会大大受影响。\n",
    "\n",
    "=> dropna\n",
    "\n",
    "`df.dropna()` 会将含有空值的所有行都删除掉。`df..dropna(how='all')` 只会删除掉整行都是空值的行。\n",
    "\n",
    "=> fillna\n",
    "\n",
    "`df.fillna(0)` 会给将空值填充为 0。但有的时候不同的列，数据类型不同，因此希望填充的值也不同。\n",
    "\n",
    "传入一个字典，可以对特定的列指定默认填充值。下面的例子中，只会对 age 和 name 两列进行填充。\n",
    "\n",
    "```python\n",
    "df.fillna({\"age\": 10, \"name\": \"unknown\"})`\n",
    "```\n",
    "\n",
    "`fillna` 默认会返回一个新的 DataFrame 对象，可以使用 `df.fillna(0, inplace=True)` 来在原地进行填充。\n",
    "\n",
    "还可以通过 `method` 参数指定填充策略。\n",
    "\n",
    "```python\n",
    "df.fillna(method='ffill')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 合并 DataFrame\n",
    "\n",
    "默认 pandas 会在两个 df 中寻找共有的列，然后以此列进行合并。\n",
    "\n",
    "```python\n",
    "merged = pd.merge(df1, df2)\n",
    "```\n",
    "\n",
    "**how**\n",
    "\n",
    "默认会采用 `inner join`，可以通过 `how` 来指定 join 的策略，可选项有 `inner`,`outer`,`left`,`right` 。 \n",
    "\n",
    "```python\n",
    "merged = pd.merge(df1, df2, how='outer')\n",
    "```\n",
    "\n",
    "**on**\n",
    "\n",
    "如果两个 DataFrame 中存在对个相同的列，可以通过 `on` 来指定使用哪一列来进行合并。\n",
    "\n",
    "```python\n",
    "merged = pd.merge(df1, df2, on='name')\n",
    "```\n",
    "\n",
    "**left_on / right_on**\n",
    "\n",
    "有的时候待合并的两个 df 中，对应的列的名字不同，此时就可以通过 `left_on` 和 `right_on` 分别指定作为合并依据的列。\n",
    "\n",
    "```python\n",
    "merged = pd.merge(df1, df2, left_on='id', right_on='uid')\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sorting and Ranking\n",
    "\n",
    "=> sort_index\n",
    "\n",
    "使用 `sort_index` 按 index 的数字或字母顺序进行排序。\n",
    "\n",
    "```python\n",
    "s = pd.Series([0, 1, 2, 3], index=['d', 'a', 'b', 'c'])\n",
    "s.sort_index()\n",
    ">>>\n",
    "a    1\n",
    "b    2\n",
    "c    3\n",
    "d    0\n",
    "dtype: int64\n",
    "```\n",
    "\n",
    "DataFrame 行和列均可以以排序：\n",
    "\n",
    "```python\n",
    "# 对 index 进行 sort\n",
    "df.sort_index()\n",
    "\n",
    "# 对 columns 进行 sort\n",
    "df.sort_index(axis=1)\n",
    "```\n",
    "\n",
    "可以明确指定使用降序排列：\n",
    "\n",
    "```python\n",
    "df.sort_index(ascending=False)\n",
    "```\n",
    "\n",
    "=> sort_values\n",
    "\n",
    "对 DataFrame 常常希望根据某一列的值来对行进行排序，此时可以使用 `by` 指出该列，还可以指定多列：\n",
    "\n",
    "```python\n",
    "df.sort_values(by='age')\n",
    "df.sort_values(by=['age', 'name'])\n",
    "```\n",
    "\n",
    "=> rank\n",
    "\n",
    "`rank` 用来计算一组数在整体中的排名。里面例子中 `-5` 最小，所以排在第一位。对于 7 因为有重复，2 个 7 分别排名为 6,7，则 (6+7)/2=6.5 为最终的排名。\n",
    "\n",
    "```python\n",
    "s = pd.Series([7, -5, 7, 1])\n",
    "s.rank()\n",
    ">>>\n",
    "0    3.5\n",
    "1    1.0\n",
    "2    3.5\n",
    "3    2.0\n",
    "dtype: float64\n",
    "```\n",
    "\n",
    "可以通过 `method` 指出排名的取法，默认是平均。\n",
    "\n",
    "```python\n",
    "s.rank(method='min')\n",
    "```\n",
    "\n",
    "|Method   | Description |\n",
    "|:--------|:------------|\n",
    "|'average'| Default: assign the average rank to each entry in the equal group.|\n",
    "|'min'    | Use the minimum rank for the whole group.|\n",
    "|'max'    | Use the maximum rank for the whole group.|\n",
    "|'first'  | Assign ranks in the order the values appear in the data.|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unique Values, Value Counts, and Membership"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['c', 'a', 'd', 'b'], dtype=object)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj = pd.Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])\n",
    "obj.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    3\n",
       "c    3\n",
       "b    2\n",
       "d    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 迭代\n",
    "\n",
    "=> itertuples\n",
    "\n",
    "这里的 row 是一个 namedtuples。\n",
    "\n",
    "```python\n",
    "df = pd.DataFrame({\"c1\": [0,1,2], \"c2\": [1,2,3]})\n",
    "\n",
    "for row in df.itertuples(index=False):\n",
    "    print(row[0], row[1])\n",
    "    print(row.c1, row.c2)\n",
    "```\n",
    "\n",
    "=> df.apply\n",
    "\n",
    "`df.apply` 接受一个函数作为参数，对每行或每列运用该函数，这个函数如果返回一个 Series，那么最终结果会是一个 DataFrame，否则结果为 Series。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    3\n",
       "2    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(lambda x: np.sum(x), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "c1    3\n",
       "c2    6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(lambda x: np.sum(x), axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 操作字符串\n",
    "\n",
    "详情参见文档：[Working with Text Data](https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>info</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>B|C|D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>B|D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>A|C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name   info\n",
       "0    a  B|C|D\n",
       "1    b    B|D\n",
       "2    c    A|C"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'name': ['a','b','c'],\n",
    "                           'info': ['B|C|D', 'B|D', 'A|C']})\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NAME</th>\n",
       "      <th>INFO</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>B|C|D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>B|D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>A|C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  NAME   INFO\n",
       "0    a  B|C|D\n",
       "1    b    B|D\n",
       "2    c    A|C"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns = df.columns.str.upper()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NAME</th>\n",
       "      <th>INFO</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>B-C-D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>B-D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>A-C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  NAME   INFO\n",
       "0    a  B-C-D\n",
       "1    b    B-D\n",
       "2    c    A-C"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['INFO'] = df['INFO'].str.replace('|', '-')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    2\n",
       "2   -1\n",
       "Name: INFO, dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['INFO'].str.find('D')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
